/*
 * Copyright (c) 2020, nwillc@gmail.com
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package com.gitee.wsl.common.svg.container

import com.gitee.wsl.common.svg.attributes.HasAttributes
import com.gitee.wsl.common.svg.attributes.HasAttributesImpl
import com.gitee.wsl.common.svg.elements.A
import com.gitee.wsl.common.svg.elements.CIRCLE
import com.gitee.wsl.common.svg.elements.Element
import com.gitee.wsl.common.svg.elements.LINE
import com.gitee.wsl.common.svg.elements.OVAL
import com.gitee.wsl.common.svg.elements.PATH
import com.gitee.wsl.common.svg.elements.POLYGON
import com.gitee.wsl.common.svg.elements.POLYLINE
import com.gitee.wsl.common.svg.elements.RECT
import com.gitee.wsl.common.svg.elements.SVGPath
import com.gitee.wsl.common.svg.elements.TEXT
import com.gitee.wsl.common.svg.ref.USE
import com.gitee.wsl.common.svg.gradient.LinearGradient
import com.gitee.wsl.common.svg.gradient.RadialGradient
import com.gitee.wsl.transform.format.Format

/**
 * An abstract container element which provides factories for general sub elements.
 */
abstract class Container(
    name: String,
    validation: Boolean,
    hasAttributes: HasAttributes = HasAttributesImpl(validation)
) : Element(name, validation, hasAttributes) {
    /**
     * Create a rect element in this svg.
     */
    fun rect(block: RECT.() -> Unit): RECT = add(RECT(validation), block)

    /**
     * Create a text element in this svg.
     */
    fun text(block: TEXT.() -> Unit): TEXT = add(TEXT(validation), block)

    /**
     * Create a circle element in this svg.
     */
    fun circle(block: CIRCLE.() -> Unit): CIRCLE = add(CIRCLE(validation), block)

    /**
     * Create a polygon element in this svg.
     */
    fun polygon(block: POLYGON.() -> Unit): POLYGON = add(POLYGON(validation), block)

    /**
     * Create a polyline element in this svg.
     */
    fun polyline(block: POLYLINE.() -> Unit): POLYLINE = add(POLYLINE(validation), block)

    /**
     * Create a ellipse element in this svg.
     */
    fun oval(block: OVAL.() -> Unit): OVAL = add(OVAL(validation), block)

    /**
     * Create a line element in this svg.
     */
    fun line(block: LINE.() -> Unit): LINE = add(LINE(validation), block)

    /**
     * Create an a reference element in this svg.
     */
    fun a(block: A.() -> Unit): A = add(A(validation), block)

    /**
     * Create an a path element in this svg.
     */
    fun pathDef(block: PATH.() -> Unit): PATH = add(PATH(validation), block)

    fun path(convert: Format<Double> = Format{ value -> value.toString() }, block: SVGPath.() -> Unit): PATH {
        val svgPath= SVGPath()
        svgPath.block()
        return pathDef {
            d = svgPath.getSVGPathD(convert)
        }
    }

    /**
     * Create a group element in this svg.
     */
    fun group(block: G.() -> Unit): G = add(G(validation), block)

    /**
     * Create a group element in this svg.
     */
    fun use(block: USE.() -> Unit): USE = add(USE(validation), block)

    fun linearGradient(block: LinearGradient.() -> Unit): LinearGradient = add(LinearGradient(validation), block)

    fun radialGradient(block: RadialGradient.() -> Unit): RadialGradient = add(RadialGradient(validation), block)
}
